<?php

/**! This library contains a simple tool to create tables for specified form.
 *   
 */

include_once("sosqlhi.php");
include_once("sodebug.php");
include_once("dzstruct.php");


function getDatatype($Col, $Driver) {
    switch ($Driver) {
        default: /* * * * pgsql * * * */
            switch ($Col->getDatatype()) {
                case SODT_INT:
                    $result = "integer";
                    break;
                case SODT_DEC:
                    $decs = $Col->getTypeProp("decs");
                    $result = "decimal";
                    if ($decs != "") $result .= "(".$decs.")";
                    break;
                case SODT_DATETIME;
                    $result = "datetime";
                    break;
                case SODT_DATE:
                case SODT_TIME:
                    $result = $Col->getDatatype();
                    break;
                default:
                    $digits = max($Col->getTypeProp("min"),
                                  $Col->getTypeProp("max"));
                    if ($digits > 0) {
                        $result = "char(".$digits.")";
                    } else {
                        $result = "text";
                    };
                    break;
            };  // switch type
    }; // switch driver

    return $result;
}; // getDatatype


function createTables($Html, $Driver = "pg") {
    $struct = new dzStruct($Html);

    $sql = new soBaSQL("CREATE\n".$struct->DbAccess, 0, "\t", "\c", "\n");
//    $sql = new soBaSQL();
//    $sql->setDbAccess($struct->DbAccess);

    $tables = $struct->Tables->getKeys();
    $kcount = count($tables);

    for ($k = 0; $k < $kcount; $k++) {
        $table = $struct->Tables->getItem($tables[$k]);
        $sql->Table = $table->Name;
        $sql->clearSection(SOBS_DATA);
        $sql->addToSection(SOBS_DATA, "*", 
                           implode(",", $table->getKeyColNames()));

        $keys = $table->KeyCols->getKeys();
        $ccount = count($keys);
        for ($c = 0; $c < $ccount; $c++) {
            $col = new dzCol();
            $col->Name = $keys[$c];
            $keycol = $table->KeyCols->getItem($keys[$c]);
            $col->Type = $keycol->Type;
            if (soExtractVal($keycol->Type, ";", 1) == SODT_DEC) {
                $col->Type.";".$keycol->Width;
            } else {
                if (soExtractVal($keycol->Type, ";", 1) == SODT_CHAR) {
                    $col->Type.";[,".$keycol->Width."]";
                };
            };
            $sql->addToSection(SOBS_DATA, $keys[$c], 
                               getDatatype($col, $Driver));
        }; // for $c

        $cols = $struct->getColNames($tables[$k], 0);
        $ccount = count($cols);
        for ($c = 0; $c < $ccount; $c++) {
            $col = $struct->Cols->getItem($cols[$c]);
            $calculated = $col->Options->getItem(DZCP_FORMULA);
            if ($calculated == "") {
                $sql->addToSection(SOBS_DATA, $col->Name, 
                                   getDatatype($col,
                                               $Driver));
            }; // if
        }; // for $c

        $sql->run();
    }; // for $k

}; // createTables    

$wd = getenv("PWD");
global $soDebugLevel;
$soDebugLevel = SOD_DET;
createTables("file://$wd/".$argv[1]);
?>
